package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.sevice.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 商品列表查询
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> queryPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue ="1" ) Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows
    ){
        return ResponseEntity.ok(goodsService.queryPage(page,rows,key, saleable));
    }
    /**
     * 保存商品
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 商品上下架
     */
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id ,@RequestParam("saleable") Boolean saleable){
        goodsService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 基于spuId查询spuDetail
     */
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuDetailBySpuId(id));
    }
    /**
     *  基于spuId查询sku列表
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(goodsService.querySkuBySpuId(id));
    }

    /**
     * 商品修改
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spuid查询spu 、spuDetail、skus
     */
    @GetMapping("spu/querySpuBySpuId/{id}")
    public ResponseEntity<SpuDTO> querySpuBySpuId(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuBySpuId(id));
    }

    /**
     * 基于sku的id集合查询sku列表数据
     * @param ids
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(goodsService.querySkuListByIds(ids));
    }
    /**
     * 扣减库存
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> updateStock(@RequestBody Map<Long, Integer> skuMap){
        goodsService.updateStock(skuMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 加库存
     * @param skuMap 商品id及数量的map
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long, Integer> skuMap){
        goodsService.plusStock(skuMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
